package com.hj.saapp.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


import com.hj.saapp.R;
import com.hj.saapp.control.adapter.ListDialogAdapter;

import java.util.ArrayList;
import java.util.List;


/**
 * 类说明：默认 List 弹出框
 *
 * @author ChengJun
 * @date Dec 17, 2014
 * @version 1.0
 */

@SuppressLint({"NewApi", "ValidFragment"})
public class ListBottomDialog extends DialogFragment implements View.OnClickListener
{
    private Context context;
    
    private TextView positiveTv;
    
    private TextView titleTv;
    
    private ListView itemLv;
    
    private OnDialogClickListener onDialogClickListener;
    
    private ListDialogAdapter listDialogAdapter;
    
    private List<String> data = new ArrayList<String>();
    
    private String title;

    LinearLayout mLinearLayout;
    
    public ListBottomDialog(Context context, int theme, List<String> data, String title)
    {
        this.context = context;
        this.data = data;
        this.title = title;
    }
    
    @Override
    public void onStart()
    {
        super.onStart();
//        DisplayMetrics dm = new DisplayMetrics();
//        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
//        getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
//
//        Window window = getDialog().getWindow();
//        window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置
//        //window.setWindowAnimations(R.style.mystyle); // 添加动画
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        View v = inflater.inflate(R.layout.view_list_bottom_dialog, container, false);
        
        positiveTv = (TextView)v.findViewById(R.id.view_list_bottom_dialog_positiveTv);
        itemLv = (ListView)v.findViewById(R.id.view_list_bottom_dialog_itemLv);
        titleTv = (TextView)v.findViewById(R.id.view_defaulting_dialog_titleTv);
        mLinearLayout = (LinearLayout) v.findViewById(R.id.view_defaulting_dialog_mLinearLayout);

        if (title != null || !"".equals(title))
        {
            titleTv.setText(title);
        }
        
        listDialogAdapter = new ListDialogAdapter((ArrayList<String>) data);
        itemLv.setAdapter(listDialogAdapter);
        
        itemLv.setOnItemClickListener(new OnItemClickListener()
        {
            
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                if (onDialogClickListener != null)
                {
                    onDialogClickListener.onItemClickListener(position, data.get(position));
                }
                dismissAllowingStateLoss();
            }
        });
        mLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissAllowingStateLoss();

            }
        });
        
        positiveTv.setOnClickListener(this);
        
        return v;
        
    }
    
    @Override
    public void onClick(View v)
    {
        if (v.getId() == positiveTv.getId())
        {
            dismissAllowingStateLoss();
        }
    }
    
    public static interface OnDialogClickListener
    {
        public void onItemClickListener(int position, String title);
        
    }
    
    public void setOnDialogClickListener(OnDialogClickListener onDialogClickListener)
    {
        this.onDialogClickListener = onDialogClickListener;
    }
    
}
